package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.data.entity.RiskEnterpriseData;
import com.css.fxfzaqbz.modules.data.queryParams.RiskEnterpriseDataQueryParams;
import com.css.fxfzaqbz.modules.data.service.RiskEnterpriseDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *  3.3.3次生灾害数据管理子模块 危化企业数据
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class RiskEnterpriseDataController {

    @Resource
    RiskEnterpriseDataService riskEnterpriseDataService ;

    /**
     * 新增
     * @param riskEnterpriseData
     * @return
     */
    @PostMapping("/saveRiskEnterpriseData")
    public RestResponse saveRiskEnterpriseData (@RequestBody RiskEnterpriseData riskEnterpriseData){
        RestResponse restResponse = null ;
        try{
            restResponse = RestResponse.succeed(riskEnterpriseDataService.saveRiskEnterpriseData(riskEnterpriseData));
        }catch (Exception e){
            log.error("新增危化企业数据失败",e);
            restResponse = RestResponse.fail("新增危化企业数据失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询 根据 企业名字 企业地址
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllRiskEnterpriseData")
    public RestResponse getAllRiskEnterpriseData(HttpServletRequest request, RiskEnterpriseDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
//            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
//            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(Integer.parseInt(PlatformPageUtils.CUR_PAGE_DEF));
            queryParams.setPageSize(Integer.parseInt(PlatformPageUtils.PAGE_SIZE_DEF));
            restResponse = RestResponse.succeed(riskEnterpriseDataService.getAllRiskEnterpriseData(queryParams));
        }catch (Exception e){
            log.error("查询危化企业数据失败!",e);
            restResponse = RestResponse.fail("查询危化企业数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询RiskEnterpriseData数据
     * @param id
     * @return
     */
    @GetMapping("/getRiskEnterpriseData/{id}")
    public RestResponse<RiskEnterpriseData> getUser(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(riskEnterpriseDataService.getRiskEnterpriseDataById(id));
        }catch (Exception e){
            log.error("获取危化企业数据失败!",e);
            restResponse = RestResponse.fail("获取危化企业数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PostMapping("/delRiskEnterpriseData/{ids}")
    public RestResponse deleteRiskEnterpriseData(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            riskEnterpriseDataService.deleteRiskEnterpriseDatas(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除危化企业数据失败!",e);
            restResponse = RestResponse.fail("删除危化企业数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param riskEnterpriseDataVO
     * @return
     */
//    @PostMapping("/updateRiskEnterpriseData")
//    public RestResponse<RiskEnterpriseDataVO> updateElevation(@RequestBody RiskEnterpriseDataVO riskEnterpriseDataVO)  {
//        RestResponse restResponse = null;
//        try{
//            restResponse = RestResponse.succeed(riskEnterpriseDataService.updateRiskEnterpriseData(elevationVO));
//        }catch (Exception e){
//            log.error("修改失败!",e);
//            restResponse = RestResponse.fail("修改失败!");
//        }
//        return restResponse;
//    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelRiskEnterpriseData")
    public void exportExcel(HttpServletResponse response ) throws IOException {
            RestResponse restResponse = null;
            try{
                List<RiskEnterpriseData> list  = riskEnterpriseDataService.getAll();
                FileUtil2.exportExcel(list, "危化企业数据", "sheet", RiskEnterpriseData.class, "RiskEnterpriseData", response);
                restResponse = RestResponse.succeed("导出成功");
            }catch (Exception e){
                log.error("导出失败",e);
                restResponse = RestResponse.fail("导出失败");
            }
        }


    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelRiskEnterpriseData")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try {
            riskEnterpriseDataService.importExcel(file);
        } catch (Exception e) {
            log.error("导入数据失败", e);
            restResponse = RestResponse.fail("导入数据失败!");
        }
        return restResponse;
    }
}
